var proProcess=new Object();

proProcess.projectId=$("#projectId").val();
proProcess.roleType=$("#roleType").val();

proProcess.init=function(basePath,page,page1,page2,projectType){
  proProcess.basePath=basePath;
  proProcess.page=page;
  proProcess.page1=page1;
  proProcess.page2=page2;
  proProcess.projectType=projectType;
  if(projectType == '1' || projectType == '4' || projectType == '5' || projectType == '7'){
    // 每个学员的培训进度
    proProcess.project_train_process();
      if(proProcess.roleType == "company_admin"){
          $(".com-select").removeClass("nav");
          window.groupTreeCheckbox.initData = proProcess.initData;
          window.groupTreeCheckbox.check = proProcess.check;
          groupTreeCheckbox.init();
          proProcess.initDepartment();
      }
      proProcess.getTrainProcess("",proProcess.projectId);
  }
  if(projectType == '2' || projectType == '4' || projectType == '6' || projectType == '7'){
    // 练习排行记录
    proProcess.project_exercise_process();

      window.groupTreeCheckbox1.initData = proProcess.initData1;
      window.groupTreeCheckbox1.check = proProcess.check1;
      groupTreeCheckbox1.init();
      proProcess.initDepartment1();
  }
  if(projectType == '3' || projectType == '5' || projectType == '6' || projectType == '7'){
    // 考试成绩列表
    proProcess.project_exam_process();

      window.groupTreeCheckbox2.initData = proProcess.initData2;
      window.groupTreeCheckbox2.check = proProcess.check2;
      groupTreeCheckbox2.init();
      proProcess.initDepartment2();
      proProcess.getExamProcess("",proProcess.projectId);
  }
  //点击事件
  proProcess.click();

}

    // 每个学员的培训进度
    proProcess.project_train_process = function(){
        var page_size=10;
        var train_process_url = appPath + "/statistics/project_statistics_list";
        proProcess.page.init("select_train_form", train_process_url, "project_train", "project_train_page", 1, page_size);
        proProcess.page.goPage(1);
        proProcess.page.list = function(dataList){
            var len = dataList.length;
            var inner = "", item;
            var roleId = "";
            var roleName = "/";
            // 组装数据
            for(var i=0; i< len; i++) {
                item = dataList[i];
                inner += '<tr >';
                inner += '<td>' + (i+1) + '</td>';
                inner +='<td>'+item['user_name']+'</td>';
                roleId = item['role_id'];
                if("-1" != roleId){
                    roleName = item['role_name'];
                }
                inner +='<td>'+roleName+'</td>';
                if(item['company_name']){
                    inner += '<td>'+item['company_name']+'</td>';
                }else{
                    inner += '<td>/</td>';
                }

                inner += '<td>'+item['dept_name']+'</td>';
                inner += '<td>'+TimeUtil.getHouAndMinAndSec(item['requirement_studytime']*60)+'</td>';
                inner += '<td>'+TimeUtil.getHouAndMinAndSec(item['total_studytime'])+'</td>';
                inner += '<td>'+item['train_process']+'</td>';
                inner +='<td>';
                inner +='<a  onclick=\"proProcess.train_record(\'' + item.project_id+ '\',\'' + item.user_id+ '\');\" class=\"a a-info\">详情</a>';
                inner +='<a  onclick=\"proProcess.train_reminder(\'' + item.project_id+ '\',\'' + item.user_id+ '\',\'' + item.user_name+ '\');\" class=\"a a-info\">督促</a>';
                inner +='</td>';
                inner += '</tr>';
            }
            return inner;
        }
    };

    //练习排行情况
    proProcess.project_exercise_process=function () {
      var page_size=10;
      var exercise_process_url = appPath + "/exercise/showProjectExerciseOrder";
      proProcess.page1.init("select_exercise_form", exercise_process_url, "project_exercise", "project_exercise_page", 1, page_size);
      proProcess.page1.goPage(1);
      proProcess.page1.list = function(dataList){
            var len = dataList.length;
            var inner = "", item;
            var roleId = "";
            var roleName = "/";
            // 组装数据
            for(var i=0; i< len; i++) {
                item = dataList[i];
                inner += '<tr >';
                inner +='<td>'+item['rownum']+'</td>';
                inner +='<td>'+item['userName']+'</td>';
                roleId = item['roleId'];
                if("-1" != roleId){
                    roleName = item['roleName'];
                }
                inner +='<td>'+ roleName +'</td>';
                inner +='<td>'+item['companyName']+'</td>';
                inner +='<td>'+item['deptName']+'</td>';
                inner +='<td>'+item['totalQuestion']+'</td>';
                inner +='<td>'+item['yetAnswered']+'</td>';
                inner +='<td>'+item['correctAnswered']+'</td>';
                inner +='<td>'+item['correctRate']+'%</td>';
                inner +='<td>'+TimeUtil.getHouAndMinAndSec(item['answerStudyTime'])+'</td>';
                inner +='<td>';
                inner +='<a  onclick=\"proProcess.exercise_record(\'' + item.projectId+ '\',\'' + item.userId+ '\');\" class=\"a a-publish\">练习详情</a>';
                inner +='</td>';
                inner += '</tr>';
            }
            return inner;
        }
    }

    //考试成绩列表
    proProcess.project_exam_process=function () {
      var page_size=10
      var exam_process_url = appPath + "/statistics/project_statistics_list1";
      proProcess.page2.init("select_exam_form", exam_process_url, "project_exam", "project_exam_page", 1, page_size);
      proProcess.page2.goPage(1);
      proProcess.page2.list = function(dataList){
        var len = dataList.length;
        var inner = "", item;
        var exam_score_info = "";
        var roleId = "";
        var roleName = "/";
          // 组装数据
        for(var i=0; i< len; i++) {
          item = dataList[i];
          inner += '<tr >';
          inner += '<td>' + (i+1) + '</td>';
          inner +='<td>'+item['user_name']+'</td>';
            roleId = item['role_id'];
            if("-1" != roleId){
                roleName = item['role_name'];
            }
          inner +='<td>'+roleName+'</td>';
            if(item['company_name']){
                inner +='<td>'+item['company_name']+'</td>';
            }else{
                inner += '<td>/</td>';
            }
          inner +='<td>'+item['dept_name']+'</td>';
          inner +='<td>'+item['exam_score']+'</td>';
          inner +='<td>'+item['subjective_score']+'</td>';
          inner +='<td>'+item['total_score']+'</td>';
          inner +='<td>'+item['exam_Time']+'</td>';   //考试耗时
          if(item['exam_status'] == '1'){
            inner +='<td><span class="text-gray">未考试</span></td>';
          }
          if(item['exam_status'] == '2'){
            inner +='<td><span class="text-green">合格</span></td>';
          }
          if(item['exam_status'] == '3'){
            inner +='<td><span class="text-red">不合格</span></td>';
          }
          inner +='<td>';
          if(item['exam_status'] == '1'){
            inner += '<a  class=\"a\">答题记录</a>';
          }else{
            inner += '<a  onclick=\"proProcess.exam_record(\'' + item.exam_no+ '\');\" class=\"a a-view\">答题记录</a>';
          }
          inner +='</td>';
          inner += '</tr>';
        }
        return inner;
      }
    }
  /* 查看培训记录*/
  proProcess.train_record = function(projectId,userId) {
    window.location.href = appPath + "/admin/projectCourseInfo/project_course_entry?projectId=" + projectId + "&userId=" + userId + "&type=1";
  };
  // 督促学习
    proProcess.train_reminder = function(projectId,userId,userName){
        var message = "<div style='padding:30px;line-height:22px;color:#1d9e3a;font-weight:300;font-size: 14px;'> 是否确定提醒"+ userName +" 学习，点击确定就是发送。 &nbsp;&nbsp;&nbsp;&nbsp;</div>";
        layer.open({
            type:1,
            title: false, // 不显示标题栏
            closeBtn: true,
            area: "300px",
            shade: 0.3,
            id: "layer-submit-exam-paper", // 设置id, 防止重复弹出
            resize: false,
            btn:['确定','取消'],
            btnAlign: "c",
            moveType: 1,
            content: message,
            btn1: function(index) {
                studyReminder(projectId,"",userId,userName);
                 layer.close(index);
            },
            btn2: function(index) {
                layer.close(index);
            }
        });
    };
  /* 查看练习记录*/
  proProcess.exercise_record = function(projectId,userId) {
    window.location.href = appPath + "/admin/projectCourseInfo/project_course_entry?projectId=" + projectId + "&userId=" + userId + "&type=2";
  }
  /* 查看答题记录*/
  proProcess.exam_record = function(examNo){
    common.goto_student_exam_answer_view(examNo);
  }

  //点击事件
  proProcess.click = function(){
    //搜索
    $("#train_search").click(function(){
      $("#userName_train").val($.trim($("#userName_train").val()));
      $("#companyIds_train").val($.trim($("#department_select").val()));
      proProcess.project_train_process();
    });
    //全部
    $("#train_all").click(function(){
      $("#userName_train").val("");
      $("#roleId").val("");
      $("#companyIds_train").val("");
      $('.select2-selection__rendered').empty();
      proProcess.project_train_process();
    });
    //搜索
    $("#exercise_search").click(function(){
      $("#userName_exercise").val($.trim($("#userName_exercise").val()));
      $("#companyIds_exercise").val($.trim($("#department_select1").val()));
      proProcess.project_exercise_process();
    });
    //全部
    $("#exercise_all").click(function(){
      $("#userName_exercise").val("");
      $("#companyIds_exercise").val("");
        $('.select2-selection__rendered').empty();
      proProcess.project_exercise_process();
    });
    //搜索
    $("#exam_search").click(function(){
      $("#userName_exam").val($.trim($("#userName_exam").val()));
      $("#companyIds_exam").val($.trim($("#department_select2").val()));
      proProcess.project_exam_process();
    });
    //全部
    $("#exam_all").click(function(){
      $("#userName_exam").val("");
      $("#companyIds_exam").val("");
        $("#examStatus").val("");
        $('.select2-selection__rendered').empty();
      proProcess.project_exam_process();
    });

    proProcess.sort = function(type){
      if(type == '1'){
        $("#sort1").addClass("active");
        $("#sort2,#sort3").removeClass("active");
        $("#orderType").val("1");
      }
      if(type == '2'){
        $("#sort2").addClass("active");
        $("#sort1,#sort3").removeClass("active");
        $("#orderType").val("2");
      }
      if(type == '3'){
        $("#sort3").addClass("active");
        $("#sort1,#sort2").removeClass("active");
        $("#orderType").val("3");
      }
      proProcess.project_exercise_process();
    }

      $('#project_role_list').on("click", "span", function(){
          $('#project_role_list').find("span").removeClass("active");
          $(this).addClass("active");
          proProcess.change_role(this, "roleId");
      });

      $('#user_project_status').on("click", "span", function(){
          $('#user_project_status').find("span").removeClass("active");
          $(this).addClass("active");
          proProcess.change_role(this, "proUserStatus");
      });
}


proProcess.export_train_info = function(){
    var params = $("#select_train_form").serialize();
    window.location.href = appPath + '/statistics/exportTrainInfo?'+ params;
};

proProcess.export_exercise_info = function(){
    var params = $("#select_exercise_form").serialize();
    window.location.href = appPath + '/exercise/exportExerciseInfo?'+ params;
};

proProcess.export_exam_info = function(){
    var params = $("#select_exam_form").serialize();
    window.location.href = appPath + '/statistics/exportExamInfo?'+ params;
};
// 切换角色更新列表
proProcess.change_role = function (obj, id) {
    var roleId = $(obj).attr("data-value");
    $("#"+id).val(roleId);
    $("#train_"+id).val(roleId);
    $("#exam_"+id).val(roleId);
    // 加载各个角色下的培训项目进度情况
    proProcess.getTrainProcess(roleId,proProcess.projectId);
    // 加载各个角色下的培训情况
    proProcess.project_train_process();
    // 加载各个角色下的练习排行情况
    proProcess.project_exercise_process();
    // 加载各个角色下的考试总体情况
    proProcess.getExamProcess(roleId,proProcess.projectId);
    // 加载各个角色下的考试情况
    proProcess.project_exam_process();
};


proProcess.getTrainProcess = function (roleId,projectId) {
    $.ajax({
        url: appPath + '/admin/project/getTrainProcess',
        dataType: 'json',
        async: false,
        type: 'post',
        data: {'projectId': projectId,'roleId':$("#roleId").val(), 'proUserStatus':$("#proUserStatus").val()},
        success: function (data) {
            var code = data.code;
            var result = data.result;
            var trainProjectProcessMap = result.trainProjectProcessMap;
            var trainProcess_0_percent = null != trainProjectProcessMap ? trainProjectProcessMap.trainProcess_0_percent : "0%";
            var trainProcess_0_100_percent = null != trainProjectProcessMap ? trainProjectProcessMap.trainProcess_0_100_percent : "0%";
            var trainProcess_100_percent = null != trainProjectProcessMap ? trainProjectProcessMap.trainProcess_100_percent : "0%";
            var value_rate_arr = [parseFloat(trainProcess_100_percent.replace("%","")),parseFloat(trainProcess_0_100_percent.replace("%","")),parseFloat(trainProcess_0_percent.replace("%",""))];
            trainProcess_pie('container_trainProcessRate','培训率对比',['培训完成率','培训未完成率','培训未开始率'],value_rate_arr,"");

            var trainProcess_100_count = null != trainProjectProcessMap ? trainProjectProcessMap.trainProcess_100_count : 0;
            var trainProcess_0_100_count = null != trainProjectProcessMap ? trainProjectProcessMap.trainProcess_0_100_count : 0;
            var trainProcess_0_count = null != trainProjectProcessMap ? trainProjectProcessMap.trainProcess_0_count : 0;
            var trainProcess_sum_count = null != trainProjectProcessMap ? trainProjectProcessMap.trainProcess_sum_count  : 0 ;
            var value_arr = [trainProcess_100_count,trainProcess_0_100_count,trainProcess_0_count];
            trainProcess_bar('container_trainProcess','培训进度对比',['培训完成','培训进行中','培训未开始'],value_arr,trainProcess_sum_count);
        }
    });
};

proProcess.getExamProcess = function (roleId,projectId) {
    $.ajax({
        url: appPath + '/admin/project/getExamProcess',
        dataType: 'json',
        async: false,
        type: 'post',
        data: {'projectId': projectId,'roleId':$("#roleId").val(), 'proUserStatus':$("#proUserStatus").val()},
        success: function (data) {
            var code = data.code;
            var result = data.result;
            var examProcessMap = result.examProcessMap;
            // 考试合格人数
            var passCount = null != examProcessMap ? examProcessMap.passCount : 0;
            // 考试不合格人数
            var noPassCountDouble = null != examProcessMap ? examProcessMap.noPassCountDouble : 0;
            // 未考试人数
            var noExamCountDouble = null != examProcessMap ? examProcessMap.noExamCountDouble : 0;
            // 总人数
            var userCount = null != examProcessMap ? examProcessMap.userCount : 0;

            var value_arr = [passCount,noPassCountDouble,noExamCountDouble];
            trainProcess_bar('container_examProcess','考试情况对比',['考试合格','考试不合格','未考试'],value_arr,userCount);

            // 考试合格率
            var passPercent = null != examProcessMap ? examProcessMap.passPercent : '0%';
            // 考试不合格率
            var noPassPercent = null != examProcessMap ? examProcessMap.noPassPercent : '0%';
            // 考试不合格率
            var noExamPercent = null != examProcessMap ? examProcessMap.noExamPercent : '0%';

            var value_rate_arr = [parseFloat(passPercent.replace("%","")),parseFloat(noPassPercent.replace("%","")),parseFloat(noExamPercent.replace("%",""))];
            trainProcess_pie('container_examProcessRate','考试合格率对比',['考试合格率','考试不合格率','未考率'],value_rate_arr,"");
        }
    });
};





// 一键督促
function trainSupervise() {

    var layerInner = "<div id='' style='padding-left: 20px;font-size: 16px;'>";
    layerInner +="<div style='line-height: 40px;'>";
    layerInner +=  "<label style='cursor: pointer;'> 培训进度 < ";
    layerInner += " <input type='text' id='minTrainProcess' class='template'   name='template' value=''/>%</label><br/>";
    layerInner += "<span style='color: red;font-size: 12px;'>（注意：请输入数字，正数或小数点即可,不带%）</span>";
    layerInner += "</div>";
    layerInner += "<div class='search-group layui-layer-close' style=' margin-top: 60px;margin-left: 80px;'><button class='btn btn-info' onclick='saveTrainSupervise(1);' style='margin-right: 10px;'>确定</button>";
    layerInner += "<button class='btn btn-info layui-layer-close' onclick='javascript:close(1);' style=''>取消</button></div>";
    layerInner += "</div>";
    layer.open({
        type: 1,
        title: '一键督促条件设置',
        shadeClose: true, // true点击遮罩层关闭
        area: ['380px', '280px'], // 弹出层大小
        scrollbar: false, // false隐藏滑动块
        content: layerInner
    });

}


function saveTrainSupervise() {
 var  minTrainProcess = $("#minTrainProcess").val();
 if("" == minTrainProcess){
     layer.msg("最小培训进度未填写");
     return;
 };

    var reg = /^\d+(?=\.{0,1}\d+$|$)/;
    if(!reg.test(minTrainProcess)){
        msg = "输入不合法";
        layer.msg(msg);
        return ;
    }
    studyReminder(proProcess.projectId,minTrainProcess,"","");
}


function studyReminder(projectId,minTrainProcess,userId,userName) {
    $.ajax({
        url: appPath + '/admin/project/studyReminder',
        dataType: 'json',
        async: false,
        type: 'post',
        data: {'projectId': projectId,'minTrainProcess':minTrainProcess,'userId':userId,'userName':userName},
        success: function (data) {
            var code = data.code;
            var result = data.result;
            layer.msg("督促成功");
            layer.close(1);
        }
    });
}


// 项目自定义排序
function sort(value, obj) {
    $("#sortBy").val(value);
    var order = obj.className
    if(order == 'sort asc'){
        $("#descAsc").val('DESC')
    }else{
        $("#descAsc").val('ASC')
    }
    // 刷新考试成绩列表
    proProcess.project_exam_process();
}